//Page Object
const { request} = require('../../utils/request')
let recordResTimeHandler = null as any;
const app = getApp<IAppOption>()
Page({
  data: {
    recordId:null,
    currentRecord:null,
    loadingVisible:true,
    emptyVoiceDialogVisible:false,
    playingAudioUrl:''
  },
  //options(Object)
  onLoad: function(options){
    let recordId = options.id;
    if(recordId){
      this.setData({
        recordId:recordId
      })
      this.getCurRecordData(recordId);
    }else {
      wx.showModal({
        title: '提示',
        content: '记录id不能为空',
        showCancel: false,
        confirmText: '确定',
        success: (res) => {
          this.jumpBackPage()
        }
      })
    }

  },
  playThisAudio(event:any){
    // console.log('playThisAudio:',event.detail.audioUrl)
    this.setData({
      playingAudioUrl: event.detail.audioUrl
    })
  },
  onShow: function(){
    // console.log('onShow',wx.getLaunchOptionsSync())
    if(this.data.recordId){
      let updateRecord = wx.getStorageSync('updateRecord');
      if(updateRecord){
        this.getCurRecordData(this.data.recordId);
      }
      
    }
  },
  onShareAppMessage(){
    let shareData = app.getDefaultShareData()
    return {
      ...shareData,
      promise: this.postShareData()
    }
  },
  onShareTimeline(){
    let shareData = app.getDefaultShareData()
    return {
      ...shareData,
      promise: this.postShareData()
    }
  },
  showLoading(){
    this.setData({
      loadingVisible:true
    })
  },
  getShareData(){
    let shareData = app.getDefaultShareData()
    let userInfo = wx.getStorageSync('userInfo')
    let userId = (userInfo && userInfo.id) || ''
    return {
      title: '我记了一篇日记，快来看看吧',
      imageUrl: shareData.imageUrl,
      path:'/pages/share/share?inviteUserId='+userId+'&shareChannel=2'+ '&id='+this.data.recordId,
    }
  },
  postShareData(){
    return new Promise((resolve)=>{
      request({
        url:'/record-share',
        method:'POST',
        data:{
          recordId: this.data.recordId,
          templateCode:'riji'
        },
        success(rData:any){
          let shareData = app.getDefaultShareData()
          let userInfo = wx.getStorageSync('userInfo')
          let userId = (userInfo && userInfo.id) || ''
          resolve({
            title: '我记了一篇日记，快来看看吧',
            imageUrl: shareData.imageUrl,
            path:'/pages/share/share?inviteUserId='+userId+'&shareChannel=2&id='+rData.id,
          })
        }
      })
    })
  },
  hideLoading(){
    this.setData({
      loadingVisible:false
    })
  },
  jumpToTemplatePage(event:any){
    let type = event.currentTarget.dataset.type
    wx.navigateTo({
      url:'/pages/singleTemplate/singleTemplate?recordId='+this.data.recordId+'&templateCode='+type
    })
  },
  changeContent(){
    wx.navigateTo({
      url:'/pages/changeContent/changeContent?id='+this.data.recordId
    })
  },
  showEmptyVoiceDialog(){
    this.setData({
      emptyVoiceDialogVisible: true
    })
  },
  hideEmptyVoiceDialog(){
    this.setData({
      emptyVoiceDialogVisible: false
    })
  },
  jumpBackPage() {
    wx.reLaunch({
      url:'/pages/index/index'
    })
  },
  getCurRecordData(recordId:string){
    // console.log('getCurRecordData recordId:',recordId)
    let that = this as any
    if(recordResTimeHandler){
      clearInterval(recordResTimeHandler)
    }
    
    this.showLoading()
    recordResTimeHandler = setInterval(()=>{
      request({
        url:'/records/'+recordId,
        success(rData:any){
          if(rData.delFlag ===1){
            clearInterval(recordResTimeHandler)
            that.hideLoading()
            wx.showModal({
              title: '提示',
              content: '该日记已被删除',
              showCancel:false,
              success(){
                that.jumpBackPage()
              }
            })
          }
          // 已完成
          if(rData.status ===2){
            clearInterval(recordResTimeHandler)
            that.hideLoading()
            that.setData({
              currentRecord:rData,
            })
          }else if(rData.status ===3){
            clearInterval(recordResTimeHandler)
            that.hideLoading()
            // 内容为空
            that.showEmptyVoiceDialog()
            setTimeout(()=>{
              that.hideEmptyVoiceDialog()
              that.jumpBackPage()
            },2000)
          }
        }
      })
    },1000)
  },
  
});